﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AtletIS.DomainModel.Entities.Main;
using AtletIS.DomainModel.DTOs;

namespace AtletIS.Repository.Repositories
{
    public static class ChipDataRepository
    {
        /// <summary>
        ///  Chipové výsledky pre check point
        /// </summary>
        public static List<ChipResultsDto> GetChipDataForCheckPoint(this IRepository<ChipData> repository, CheckPoint checkPoint)
        {
            #region Queryable

            var chipData = repository.GetRepository<ChipData>().Queryable();
            var checkPoints = repository.GetRepository<CheckPoint>().Queryable();
            var disciplineList = repository.GetRepository<DisciplineList>().Queryable();
            var competitions = repository.GetRepository<Competition>().Queryable();
            var chipTags = repository.GetRepository<ChipTag>().Queryable();
            var performances = repository.GetRepository<Performance>().Queryable();
            var clubs = repository.GetRepository<Club>().Queryable();
            var countries = repository.GetRepository<Country>().Queryable();

            #endregion

            List<ChipResultsDto> chipResultsDtos = (
                from cd in chipData
                join cp in checkPoints on new { a = cd.PcId, b = cd.LapCount } equals new { a = cp.PcId, b = cp.LapCount }
                join dl in disciplineList on new { c = cp.DisciplineListId.Value, d = cd.MeasurementId } equals new { c = dl.ID, d = dl.MeasurementId.Value }
                join c in competitions on new { a = dl.CompetitionId.Value, b = cd.CompetitionId } equals new { a = c.ID, b = c.ID }
                join ct in chipTags on new { a = cd.ChipTag, b = cd.ChipTagsGroupId.Value } equals new { a = ct.ChipTag1, b = ct.ChipTagsGroupId }
                join p in performances on new { a = ct.StartNumber, b = dl.ID } equals new { a = p.StartNumber, b = p.DisciplineListId.Value }
                join cou in countries on p.CountryId equals cou.ID
                join cl in clubs on p.Club equals cl.ID into t from clu in t.DefaultIfEmpty()
                orderby cd.ChipDateTime ascending
                where cp.ID == checkPoint.ID && cd.CompetitionId == checkPoint.DisciplineList.CompetitionId.Value
                select new ChipResultsDto()
                {
                    CompetitionId = cd.CompetitionId,
                    MeasurementId = cd.MeasurementId,
                    DataIndex = cd.DataIndex,
                    PcId = cd.PcId,
                    LapCount = cd.LapCount.Value,
                    ChipTag = cd.ChipTag,
                    ChipTagsGroupId = cd.ChipTagsGroupId.Value,
                    ChipDateTime = cd.ChipDateTime.Value,

                    PerformanceId = p.ID,
                    DisciplineListId = dl.ID,

                    StartNumber = p.StartNumber.Value,
                    Surname = p.Surname,
                    FirstName = p.FirstName,
                    BirthDate = p.Birthdate.Value,
                    CountryId = p.CountryId,
                    Club = p.Club,
                    SMS = p.SMS,

                    CheckPointName = cp.Name,
                    DisciplineListName = dl.Name,
                    ClubName = clu.Name,
                    CountryName = cou.Name
                }).ToList();

            return chipResultsDtos;
        }
        
    }
}
